# 给你二叉树的根结点 root ，请你将它展开为一个单链表： 
# 
#  
#  展开后的单链表应该同样使用 TreeNode ，其中 right 子指针指向链表中下一个结点，而左子指针始终为 null 。 
#  展开后的单链表应该与二叉树 先序遍历 顺序相同。 
#  
# 
#  
# 
#  示例 1： 
# 
#  
# 输入：root = [1,2,5,3,4,null,6]
# 输出：[1,null,2,null,3,null,4,null,5,null,6]
#  
# 
#  示例 2： 
# 
#
# 输入：root = []
# 输出：[]
#  
# 
#  示例 3： 
# 
#  
# 输入：root = [0]
# 输出：[0]
#  
# 
#  
# 
#  提示： 
# 
#  
#  树中结点数在范围 [0, 2000] 内 
#  -100 <= Node.val <= 100 
#  
# 
#  
# 
#  进阶：你可以使用原地算法（O(1) 额外空间）展开这棵树吗？ 
#  Related Topics 栈 树 深度优先搜索 链表 二叉树 
#  👍 946 👎 0


from typing import List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


# leetcode submit region begin(Prohibit modification and deletion)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        curr = root
        while curr:
            if curr.left:
                left = curr.left
                next = left
                while next.right:
                    next = next.right
                next.right = curr.right
                curr.right = left
            curr = curr.right






# leetcode submit region end(Prohibit modification and deletion)


def log(*args, **kwargs):
    print(*args, **kwargs)


def link_to_list(node: TreeNode) -> List[int]:
    ans = []
    while node:
        ans.append(node.val)
        node = node.right
    return ans


# 1. 递归, 前序遍历,将节点储存在list中, 解除左子指针
#     def flatten(self, root: TreeNode) -> None:
#         """
#         Do not return anything, modify root in-place instead.
#         """
#         ans = []
#
#         def preorder(node: TreeNode):
#             if node:
#                 ans.append(node)
#                 preorder(node.left)
#                 preorder(node.right)
#
#         preorder(root)
#         for i in range(1, len(ans)):
#             prev, curr = ans[i - 1], ans[i]
#             prev.left = None
#             prev.right = curr
# 2. 前驱节点
# 找到左子树的最右节点, 连接右子树
# 节点左子树为空, 右子树链接原左子树
# 处理链表后续节点
#
#     def flatten(self, root: TreeNode) -> None:
#         """
#         Do not return anything, modify root in-place instead.
#         """
#         curr = root
#         while curr:
#             if curr.left:
#                 left = curr.left
#                 next = curr.left
#                 while next.right:
#                     next = next.right
#                 next.right = curr.right
#                 curr.left = None
#                 curr.right = left
#             curr = curr.right

if __name__ == '__main__':
    s = Solution()
    # 输入：root = [1,2,5,3,4,null,6]
    # 输出：[1,null,2,null,3,null,4,null,5,null,6]
    t1 = TreeNode(1, TreeNode(2, TreeNode(3), TreeNode(4)), TreeNode(5, None, TreeNode(6)))
    s.flatten(t1)
    r1 = link_to_list(t1)
    e1 = [1, 2, 3, 4, 5, 6]
    assert r1 == e1, r1
